package com.openxt.nbzj.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.openxt.nbzj.R;
import com.openxt.nbzj.fragment.OpenxtBaseActivity;
import com.openxt.nbzj.model.ConfigModel;
import com.openxt.nbzj.model.RemotingModel;
import com.openxt.nbzj.remoting.RemotingEx;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONObject;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016/7/5.
 */
public class NickNameActivity extends OpenxtBaseActivity implements RemotingEx.OnActionListener {

    @BindView(R.id.nickname)
    EditText nickname;
    Context mContext = null;
    Handler mHandler = new Handler();
    RemotingModel remotingModel = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setBaseStyle(R.layout.nick_name_activity, "修改昵称");
        ButterKnife.bind(this);
        mContext = this;
        remotingModel = new RemotingModel(mContext);

        setupRight();
        try {
            String val = Utils.getLocalStorage(mContext, ConfigModel.LOGIN_NATIVE);
            if (StringUtil.isNull(val))
                return;
            JSONObject info = JSON.parseObject(val);
            final JSONObject UserInfo = info.getJSONObject("UserInfo");
            nickname.setText(UserInfo.getString("NICKNAME"));
        }catch (Exception e){

        }

        setupRight();
    }

    private void setupRight() {
        LinearLayout right = getRight();
        right.setVisibility(View.VISIBLE);
        right.removeAllViews();

        View view = LayoutInflater.from(this).inflate(R.layout.right_tv_btn, null);
        view.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        right.addView(view);

        TextView btn = (TextView)view.findViewById(R.id.right_tv_btn);
        btn.setText("完成");

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideInputMethod();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        doUpdateNickName();
                    }
                }, 32);
            }
        });
    }

    public void doUpdateNickName(){
        String $nickname = StringUtil.getValue(nickname.getText().toString());
        if(StringUtil.isNull($nickname)){
            showMsg("请输入昵称");
            return;
        }

        remotingModel.doCustomerUpdatenickname(this, true, $nickname);
    }

    @Override
    public void onActionCompleted(String reVal, String actionName) {
        if(actionName.startsWith(ConfigModel.CUSTOMER_UPDATENICKNAME)){
            if("true".equalsIgnoreCase(reVal)) {
                final String $nickname = actionName.replace(ConfigModel.CUSTOMER_UPDATENICKNAME + "$", "");
                showMsg("昵称修改成功");
                try {
                    String val = Utils.getLocalStorage(mContext, ConfigModel.LOGIN_NATIVE);
                    if (StringUtil.isNull(val))
                        return;
                    JSONObject info = JSON.parseObject(val);
                    final JSONObject UserInfo = info.getJSONObject("UserInfo");
                    UserInfo.put("NICKNAME", $nickname);

                    Utils.setLocalStorage(mContext, ConfigModel.LOGIN_NATIVE, info.toString());

                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 32);
                } catch (Exception e) {
                }
            }else
                showMsg("昵称修改失败");
        }
    }

    @Override
    public void onActionFailed(String errorVal, String actionName) {
        showMsg(errorVal);
    }
}